<?php

declare(strict_types=1);

namespace App\Controller;

use App\Service\OrderService;
use Hyperf\Di\Annotation\Inject;

use function Hyperf\Config\config;

class IndexController extends AbstractController
{
    #[Inject]
    protected OrderService $orderService;

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
            'nacos_config' => config('nacos_config'),
        ];
    }

    public function health()
    {
        $result = [
            'code' => 1,
            'data' => [],
            'msg' => 'success',
        ];
        try {
            $result['data'] = [
                // RPC 调用实例 创建订单 分布式事务
                'rpc_order_test' => $this->orderService->createOrder(),
            ];
        } catch (\Throwable $th) {
            $result['code'] = 0;
            $result['msg'] = $th->getMessage();
        }

        return $result;
    }
}
